//
//  YWNewAssetInventoryEquipmentTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/10/26.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWNewAssetInventoryEquipmentTableViewCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "YWEquipment.h"
#import "YWDBTool.h"
#import "YWAssetCheckRecordDetails.h"
#import "YWEquipmentType.h"
#import "YWCommonTool.h"
#import "YYLabel.h"

@interface YWNewAssetInventoryEquipmentTableViewCell()
/*设备*/
@property (nonatomic, strong) YYLabel * equipmentLabel;
/*应有数量*/
@property (nonatomic, strong)  YYLabel * dueNumberLabel;
/*实际数量*/
@property (nonatomic, strong)  YYLabel * realLabel;
/*减少数量*/
@property (nonatomic, strong) UIButton * cutButton;
/*增加数量*/
@property (nonatomic, strong) UIButton * addButton;
/*显示、输入数量*/
@property (nonatomic, strong) UITextField * numbleTextField;
@end

@implementation YWNewAssetInventoryEquipmentTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setCheckDetail:(YWAssetCheckRecordDetails *)checkDetail{
    _checkDetail = checkDetail;
    _numbleTextField.tag = 100+checkDetail.detailId;
    NSString * equipmentTypeName = [[YWDBTool shareDBTool] equipmentTypeNameWithTypeId:_checkDetail.equipmentType.equipmentTypeId];
    _equipmentLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"设备: " firstColor:[UIColor customTextGrayColor] secondText:equipmentTypeName secondColor:[UIColor blackColor] font:kContentFont];

    NSString *referQuantity = [NSString stringWithFormat:@"%ld",(long)_checkDetail.referQuantity];
    _dueNumberLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"应有数量: " firstColor:[UIColor customTextGrayColor] secondText:referQuantity secondColor:[UIColor blackColor] font:kContentFont];
    _realLabel.text = @"实际数量:";
    _numbleTextField.text = [NSString stringWithFormat:@"%ld",(long)_checkDetail.checkQuantity];
}

- (void)setUpSubviews{
    [self.equipmentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    [self.dueNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.equipmentLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.bottom.mas_equalTo(self.contentView).offset(-8);
    }];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.dueNumberLabel.mas_centerY);
        make.right.mas_equalTo(self.contentView).offset(-8);
        make.size.mas_equalTo(CGSizeMake(23, 23));
    }];
    [self.numbleTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.dueNumberLabel.mas_centerY);
        make.right.mas_equalTo(self.addButton.mas_left).offset(-8);
        make.size.mas_equalTo(CGSizeMake(40, 23));
    }];
    [self.cutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.dueNumberLabel.mas_centerY);
        make.right.mas_equalTo(self.numbleTextField.mas_left).offset(-8);
        make.size.mas_equalTo(CGSizeMake(23, 23));
    }];
    [self.realLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.dueNumberLabel.mas_centerY);
        make.right.mas_equalTo(self.cutButton.mas_left).offset(-8);
    }];
}

- ( YYLabel *)equipmentLabel{
    if (!_equipmentLabel) {
         YYLabel *label = [[ YYLabel alloc] init];
        _equipmentLabel = label;
        [self.contentView addSubview:_equipmentLabel];
    }
    return _equipmentLabel;
}

- ( YYLabel *)dueNumberLabel{
    if (!_dueNumberLabel) {
         YYLabel *label = [[ YYLabel alloc] init];
        _dueNumberLabel = label;
        [self.contentView addSubview:_dueNumberLabel];
    }
    return _dueNumberLabel;
}

- ( YYLabel *)realLabel{
    if (!_realLabel) {
         YYLabel *label = [[ YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        _realLabel = label;
        [self.contentView addSubview:_realLabel];
    }
    return _realLabel;
}

- (UIButton*)cutButton{
    if (!_cutButton) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"common_delete"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(cutButtonNumberEvent:) forControlEvents:UIControlEventTouchUpInside];
        _cutButton = button;
        [self.contentView addSubview:_cutButton];
    }
    return _cutButton;
}

- (UITextField*)numbleTextField{
    if (!_numbleTextField) {
        UITextField * textField = [[UITextField alloc] init];
        [textField addTarget:self action:@selector(textFieldEditChange:) forControlEvents:UIControlEventEditingChanged];
        textField.keyboardType = UIKeyboardTypeNumberPad;
        textField.textAlignment = NSTextAlignmentCenter;
        textField.clipsToBounds = YES;
        textField.layer.cornerRadius = 3.0;
        textField.layer.borderWidth = 1.0;
        textField.layer.borderColor = [UIColor customTextGrayColor].CGColor;
        _numbleTextField = textField;
        [self.contentView addSubview:_numbleTextField];
    }
    return _numbleTextField;
}

- (UIButton*)addButton{
    if (!_addButton) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"common_add"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(addButtonNumberEvent:) forControlEvents:UIControlEventTouchUpInside];
        _addButton = button;
        [self.contentView addSubview:_addButton];
    }
    return _addButton;
}

- (void)textFieldEditChange:(id)sender{
    if (_checkDetail.detailId == _numbleTextField.tag-100) {
        if (_numbleTextField.text.length > 4){
            [YWCommonTool showTextHUDWithContent:@"输入数量不能超过10000"];
            NSRange rangeRange = [_numbleTextField.text rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 4)];
            _numbleTextField.text = [_numbleTextField.text substringWithRange:rangeRange];
        }
        
        NSString *content = _numbleTextField.text;
        NSInteger count = [content integerValue];
        _checkDetail.checkQuantity = count;
        _numbleTextField.text = [NSString stringWithFormat:@"%ld",(long) count];
    }
}

- (void)cutButtonNumberEvent:(UIButton*)sender{
    if (_checkDetail.checkQuantity > 0) {
        _checkDetail.checkQuantity = _checkDetail.checkQuantity -1;
        _numbleTextField.text = [NSString stringWithFormat:@"%ld",(long) _checkDetail.checkQuantity];
    }else{
         _checkDetail.checkQuantity = 0;
    }
}

- (void)addButtonNumberEvent:(UIButton*)sender{
    _checkDetail.checkQuantity = _checkDetail.checkQuantity + 1;
    _numbleTextField.text = [NSString stringWithFormat:@"%ld",(long)_checkDetail.checkQuantity];
}

@end
